package com.mocoop.ui;

import com.mocoop.activity.R;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PromptView extends RelativeLayout {
	private ImageView imageView;
	private ProgressBar progressBar;
	private TextView textView;
	private boolean isAutoHide;
	private Handler autoHandler;
	private final int hideTime = 1000;

	public PromptView(Context context) {
		super(context);

		LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.alert_dialog, this);
		imageView = (ImageView) findViewById(R.id.promptView);
		progressBar = (ProgressBar) findViewById(R.id.promptLoading);
		textView = (TextView) findViewById(R.id.promptClues);
		autoHandler = new Handler();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			if (isAutoHide) {
				hidePrompt();
			}
		}
		return true;
	}

	
	Runnable hideRunnable = new Runnable() {
		@Override
		public void run() {
			hidePrompt();
		}
	};

	public void setTypeAndClues(String type, String clues) {
		isAutoHide = true;
		this.setVisibility(VISIBLE);
		textView.setText(clues);
		imageView.setVisibility(VISIBLE);
		progressBar.setVisibility(GONE);
		if ("Succeed".equals(type)) {
			imageView.setBackgroundResource(R.drawable.prompt_success);
		} else if ("Failed".equals(type)) {
			imageView.setBackgroundResource(R.drawable.prompt_error);
		} else if ("Info".equals(type)) {
			imageView.setBackgroundResource(R.drawable.prompt_info);
		} else if ("Normal".equals(type)) {
			isAutoHide = false;
			imageView.setVisibility(GONE);
			progressBar.setVisibility(VISIBLE);
		}
		if (isAutoHide) {
			autoHandler.postDelayed(hideRunnable, hideTime);
		}
	}

	public void hidePrompt() {
		this.setVisibility(GONE);
	}

}
